package top.suven.base.http.admin.sec.controller;

import top.suven.base.common.excel.ColumnBox;
import top.suven.base.common.excel.ExportUtils;
import top.suven.base.core.mybatis.PageData;
import top.suven.base.http.admin.frame.config.ResponseResult;
import top.suven.base.http.admin.sec.model.AdminRole;
import top.suven.base.http.admin.sec.service.AdminRoleService;
import top.suven.base.http.admin.sec.vo.request.AdminRoleRequestVo;
import top.suven.base.http.admin.sec.vo.response.AdminRoleResponseVo;
import top.suven.base.http.admin.sec.vo.valid.SecValidGroup;
import top.suven.base.http.enums.EnableStatusEnum;
import top.suven.base.http.enums.StatusEnum;
import top.suven.base.http.enums.SysMsgEnumType;
import top.suven.base.util.utilize.BeanUtil;
import top.suven.base.util.utilize.DateUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;


/**   
 * @Title: AdminRoleController.java
 * @Description: 操作员角色关系的控制服务类
 * @author lixiangling
 * @date   2018/08/13 17:07:07
 * @version V1.0
 * ----------------------------------------------------------------------------
 *  modifyer    modifyTime                 comment
 *
 * ----------------------------------------------------------------------------
 *
 */
@Controller
@RequestMapping("/sec/adminRole")
public class AdminRoleController {
    private final static Logger logger = LoggerFactory.getLogger(AdminRoleController.class);
	@Autowired
	private AdminRoleService adminRoleService;
    /**
     * @Title: 跳转到操作员角色关系主界面
     * @Description:adminRoleRequestVo @{Link AdminRoleRequestVo}
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/13 17:07:07
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    @RequestMapping(value = "")
    public String index(){
        return "sec/adminRole_index";
    }
    /**
     * @Title: 获取操作员角色关系分页信息
     * @Description:adminRoleRequestVo @{Link AdminRoleRequestVo}
     * @param
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/13 17:07:07
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    @ResponseBody
    @RequestMapping(value = "list")
    public PageData<AdminRoleResponseVo> list(@Validated(value = {SecValidGroup.SecQueryGroup.class}) AdminRoleRequestVo adminRoleRequestVo){
        AdminRole adminRole = new AdminRole();
        BeanUtil.copyProperties(adminRoleRequestVo, adminRole);
        return adminRoleService.getAdminRolePage(adminRole, adminRoleRequestVo.getPage(), adminRoleRequestVo.getLimit());
    }
   /**
    * @Title: 保存操作员角色关系信息
    * @Description:adminRoleRequestVo @{Link AdminRoleRequestVo}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/13 17:07:07
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @ResponseBody
    @RequestMapping(value = "save")
    public ResponseResult save(@Validated(value = {SecValidGroup.SecFormGroup.class}) AdminRoleRequestVo adminRoleRequestVo){
        AdminRole adminRole = new AdminRole();
        BeanUtil.copyProperties(adminRoleRequestVo, adminRole);
        if(adminRole.getAdminRoleId() != null){
            adminRoleService.updAdminRole(adminRole);
        }else{
            adminRole.setStatus(StatusEnum.VALID.getIndex());
            adminRole.setEnableStatus(EnableStatusEnum.ENABLE.getIndex());
            adminRoleService.addAdminRole(adminRole);
        }
        return ResponseResult.ok();
    }
   /**
    * @Title: 查看操作员角色关系信息
    * @Description:adminRoleRequestVo @{Link AdminRoleRequestVo}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/13 17:07:07
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @RequestMapping(value = "detail")
    public String detail(ModelMap modelMap, Long id){
        AdminRole adminRole = null;
        if(id > 0){
            adminRole = adminRoleService.getAdminRole(id);
        }
        modelMap.put("adminRole", adminRole);
        return "sec/adminRole_detail";
    }
    /**
     * @Title: 跳转操作员角色关系编辑界面
     * @Description:id @{Link Long}
     * @param
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/13 17:07:07
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    @RequestMapping(value = "edit")
    public String edit(ModelMap modelMap, @RequestParam(defaultValue = "0") Long id){
        AdminRole adminRole = null;
        if(id > 0){
            adminRole = adminRoleService.getAdminRole(id);
        }
        modelMap.put("adminRole", adminRole);
        return "sec/adminRole_edit";
    }
    /**
     * @Title: 删除操作员角色关系信息
     * @Description:id @{Link Long}
     * @param
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/13 17:07:07
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    @ResponseBody
    @RequestMapping("del")
    public ResponseResult del( @RequestParam(value = "id") Long[] ids){
        if (ids == null || ids.length == 0) {
            return ResponseResult.error(SysMsgEnumType.SYS_WEB_CHECK_INFO_FAIL);
        }
        adminRoleService.del(ids);
        return ResponseResult.ok();
    }
   /**
    * @Title: 启用操作员角色关系信息
    * @Description:id @{Link Long}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/13 17:07:07
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @ResponseBody
    @RequestMapping(value = "turnOn")
    public ResponseResult turnOn(@RequestParam(value = "id") Long[] ids){
        if (ids == null || ids.length == 0) {
            return ResponseResult.error(SysMsgEnumType.SYS_WEB_CHECK_INFO_FAIL);
        }
        adminRoleService.turnOn(ids);
        return ResponseResult.ok();
    }
   /**
    * @Title: 禁用操作员角色关系信息
    * @Description:id @{Link Long}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/13 17:07:07
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @ResponseBody
    @RequestMapping(value = "turnOff")
    public ResponseResult turnOff(@RequestParam(value = "id") Long[] ids){
        if (ids == null || ids.length == 0) {
            return ResponseResult.error(SysMsgEnumType.SYS_WEB_CHECK_INFO_FAIL);
        }
        adminRoleService.turnOff(ids);
        return ResponseResult.ok();
    }

    /**
    * @Title: 导出操作员角色关系信息
    * @Description:id @{Link Long}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/13 17:07:07
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @RequestMapping("export")
    public void export(HttpServletResponse response, AdminRoleRequestVo adminRoleRequestVo){
        AdminRole adminRole = new AdminRole();
        BeanUtil.copyProperties(adminRoleRequestVo, adminRole);
        PageData<AdminRoleResponseVo> adminRolePage = adminRoleService.getAdminRolePage(adminRole, adminRoleRequestVo.getPage(), adminRoleRequestVo.getLimit());
        List<AdminRoleResponseVo> data = adminRolePage.getData();

        ColumnBox columnBox = ColumnBox.create()
                
			.add("adminRoleId", "操作员用户组管理ID")
			.add("roleCode", "用户组编码")
			.add("adminCode", "管理员ID")
			.add("status", "状态")
			.add("reorder", "排序")
			.add("remark", "备注")
			.add("enableStatus", "启用状态 启用-1、禁用-0");

        try {
            ExportUtils.write(String.format("操作员角色关系_%s.xls",DateUtil.date("yyyy-MM-dd",new Date().getTime())), response, data, columnBox);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}